from django.shortcuts import render, redirect, reverse
from django.http import JsonResponse
import string
import random
from django.core.mail import send_mail
from .models import CaptchaModel
from django.views.decorators.http import require_http_methods
from .forms import RegisterForm, LoginForm
from django.contrib.auth import get_user_model, login, logout
# from django.contrib.auth.models import User


User = get_user_model()


@require_http_methods(['GET', 'POST'])
def cclogin(request):
    if request.method == 'GET':
        return render(request, 'login.html')
    else:
        form = LoginForm(request.POST)
        if form.is_valid():
            email = form.cleaned_data.get('email')
            password = form.cleaned_data.get('password')
            remember = form.cleaned_data.get('remember')
            print(email, password, remember)
            user = User.objects.filter(email=email).first()
            if user and user.check_password(password):
                # 登录
                login(request, user)
                if not remember:
                    # 如果没有点击记住我，那么就要设置过期时间为0，即关闭浏览器后就会过期
                    request.session.set_expiry(0)
                # 如果点击了，那么就什么都不做，自动使用默认的两周时间
                return redirect('/')
        else:
            print('邮箱密码错误!')
            # form.add_error('email', '邮箱或者密码错误')
            # return render(request, 'login.html', context={'form': form})
            return redirect(reverse('ccauth:login'))


def cclogout(request):
    logout(request)
    return redirect('/')


@require_http_methods(['GET', 'POST'])
def register(request):
    if request.method == 'GET':
        return render(request, 'register.html')
    else:
        form = RegisterForm(request.POST)
        if form.is_valid():
            email = form.cleaned_data.get('email')
            username = form.cleaned_data.get('username')
            password = form.cleaned_data.get('password')
            # 不加密密码存储
            # user = User(username=username, email=email, password=password)
            # user.save()
            # 加密密码存储
            User.objects.create_user(username=username, email=email, password=password)
            # 路由跳转到登录页面
            return redirect(reverse('ccauth:login'))
        else:
            print(form.errors)
            # 重新跳转到注册页面
            return redirect(reverse('ccauth:register'))
            # return render(request, 'register.html', {'form': form})


def send_email_captcha(request):
    email = request.GET.get('email')
    if not email:
        return JsonResponse({'code': 400, 'msg': '邮箱地址不能为空！'})
    # 生成验证码（取四位阿拉伯数字）
    # 该函数返回数据格式（列表）：['0', '3',  '5', '2']
    captcha = "".join(random.sample(string.digits, 4))
    # 存储到数据库中
    CaptchaModel.objects.update_or_create(email=email, defaults={'captcha': captcha})
    print(captcha)
    send_mail('Django博客注册验证码', message=f'您的注册验证码是{captcha}', recipient_list=[email], from_email=None)
    return JsonResponse({'code': 200, 'msg': '邮箱验证码已发送'})
